<script>
	const qs = require('qs');
	export default {
		onLaunch: function() {
			console.log('App Launch')

			wx.login({
				success: function(res) {
					// console.log(res)
					wx.setStorageSync('code', res.code)
				}
			})

		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		onLoad: function() {

		},
		async mounted() {
			await this.getCode();
			this.getToken();
		},
		methods: {
			//获取存储code
			getCode() {
				let that = this;
				return new Promise(function(resolve, reject) {
					wx.login({
						success: function(res) {
							wx.setStorageSync('code', res.code)
							resolve();
						}
					})
				})

			},
			getToken() {
				let that = this;
				wx.getSetting({
					success(res) {
						console.log(res)
						if (res.authSetting['scope.userInfo']) {
							const datas = qs.stringify({
								code: wx.getStorageSync('code'),
								wx: 2
							})

							const requestParam = {
								url: '/weixin/ma/login',
								method: "POST",
								data: datas,
								callBack: (data) => {
									console.log(data)
									uni.setStorageSync('Token', data.data);
								}
							}
							that.$http.login(requestParam);
						}
					}
				})
			},
			getPhoneHeight() {
				let that = this
				uni.getSystemInfo({
					success: function(res) {
						that.globalData.phoneHeight = res.statusBarHeight
					}
				})
			}
		}

	}
</script>

<style>
	@import "/wxcomponents/vant/dist/common/index.wxss";
	/*每个页面公共css */
</style>
